<template>
  <q-page padding>
    <div class="items-center row q-gutter-md" style="margin-bottom: 10px">
      <q-input
        dense
        outlined
        style="width: 20%"
        v-model="queryParams.role_code"
        :label="t('Role') + t('Code')"
      />
      <q-input
        dense
        outlined
        style="width: 20%"
        v-model="queryParams.role_name"
        :label="t('Role') + t('Name')"
      />
      <q-btn color="primary" @click="handleSearch" :label="$t('Search')" />
      <q-btn color="primary" @click="resetSearch" :label="$t('Reset')" />
    </div>
    <q-space />
    <q-table
      dense
      row-key="id"
      separator="cell"
      :rows="tableData"
      :columns="columns"
      v-model:pagination="pagination"
      :rows-per-page-options="pageOptions"
      :loading="loading"
      @request="onRequest"
    >
      <template v-slot:top>
        <q-btn
          color="primary"
          @click="showAddForm()"
          :label="$t('Add') + ' ' + $t('Role')"
        />
      </template>
      <template v-slot:body-cell-created_at="props">
        <q-td :props="props">
          {{ showDateTime(props.row.created_at) }}
        </q-td>
      </template>
      <template v-slot:body-cell-updated_at="props">
        <q-td :props="props">
          {{ showDateTime(props.row.updated_at) }}
        </q-td>
      </template>
      <template v-slot:body-cell-actions="props">
        <q-td :props="props">
          <div class="q-gutter-xs">
            <q-btn
              color="primary"
              size="sm"
              @click="showEditForm(props.row)"
              :label="$t('Edit')"
            />
            <q-btn
              color="warning"
              size="sm"
              @click="showRolePermissionDialog(props.row)"
              :label="$t('Permission')"
            />
            <q-btn
              color="negative"
              size="sm"
              @click="handleDelete(props.row)"
              :label="$t('Delete')"
            />
          </div>
        </q-td>
      </template>
      <template v-slot:loading>
        <q-inner-loading :showing="loading">
          <q-spinner-gears size="50px" color="primary" />
        </q-inner-loading> </template
    ></q-table>
    <RoleRecordDetail ref="recordDetailDialog" @handleFinish="handleFinish" />
    <RolePermissionDialog
      ref="rolePermissionDialog"
      @handleFinish="handleFinish"
    />
  </q-page>
</template>

<script setup>
import useTableData from "src/composables/useTableData";
import { computed, onMounted, ref } from "vue";
import { useI18n } from "vue-i18n";
import RoleRecordDetail from "./RoleRecordDetail.vue";
import { FormatDateTime } from "src/utils/date";
import RolePermissionDialog from "./RolePermissionDialog.vue";

const { t } = useI18n();
const url = {
  list: "role/get-role-list",
  delete: "role/delete-role-by-id",
};

const columns = computed(() => {
  return [
    {
      name: "sort",
      align: "center",
      label: t("Sort"),
      field: "sort",
    },
    {
      name: "role_code",
      align: "center",
      label: t("Role") + t("Code"),
      field: "role_code",
    },
    {
      name: "role_name",
      align: "center",
      label: t("Role") + t("Name"),
      field: "role_name",
    },
    {
      name: "created_at",
      align: "center",
      label: t("CreatedAt"),
      field: "created_at",
    },
    {
      name: "created_by",
      align: "center",
      label: t("CreatedBy"),
      field: "created_by",
    },
    {
      name: "updated_at",
      align: "center",
      label: t("UpdatedAt"),
      field: "updated_at",
    },
    {
      name: "updated_by",
      align: "center",
      label: t("UpdatedBy"),
      field: "updated_by",
    },
    {
      name: "actions",
      align: "center",
      label: t("Actions"),
      field: "actions",
    },
  ];
});

const {
  pagination,
  queryParams,
  pageOptions,
  loading,
  tableData,
  recordDetailDialog,
  onRequest,
  getTableData,
  handleSearch,
  resetSearch,
  showAddForm,
  showEditForm,
  handleFinish,
  handleDelete,
} = useTableData(url);

onMounted(() => {
  getTableData();
});
const rolePermissionDialog = ref(null);
const showRolePermissionDialog = (row) => {
  rolePermissionDialog.value.show(row);
};
const showDateTime = computed(() => {
  return (datetime) => {
    return FormatDateTime(datetime);
  };
});
</script>

<script>
import { defineComponent } from "vue";

export default defineComponent({
  name: "RoleComp",
});
</script>
